define(function (require) {

    require('../../../c/base/index');

    require('../../../c/nav/index');

    var firstTime = true, infoFirstTime = true;

    var shopBank = {
        _day: "",
        init: function () {
            //初始化菜单选中效果
            $(".fa-circle-o").each(function () {
                if ($(this).parents("a").text() == "店铺财务状况") {
                    $(this).parents("a").parents("li").addClass("active");
                }
            });
            this.initTable();
            this.initInfoTable();
            this.initValidator();
            this.bindEvent();
        },
        /**
         * 执行查询操作.
         */
        doSearch: function () {
            $("#J_Search").data('bootstrapValidator').validate();
            if ($("#J_Search").data('bootstrapValidator').isValid()) {
                $('#shop_bank_table').DataTable().ajax.reload();
            }
        },
        /**
         * 初始化的列表
         */
        initTable: function () {
            var self = this;
            $("#shop_bank_table").dataTable({
                "oLanguage": CRM.pageConfig,
                "sScrollX": "100%",   //表格的宽度
                "sScrollXInner": "100%",   //表格的内容宽度
                "bScrollCollapse": true,  //当显示的数据不足以支撑表格的默认的高度时，依然显示纵向的滚动条。(默认是false)
                "bPaginate": true,  //是否显示分页
                "bLengthChange": false,  //每页显示的记录数
                "bFilter": false, //搜索栏
                "bSort": false, //是否支持排序功能
                "bInfo": false, //显示表格信息
                "bAutoWidth": false,  //自适应宽度
                "bServerSide": true,//开启服务端处理
                "sAjaxSource": CRM.getOuterUrl('/centralbank/caiwu/bankSummary'),
                "fnInitComplete": function () {
                    this.fnAdjustColumnSizing(true);
                },
                "fnServerData": function (sSource, aoData, fnCallback) {
                    if (firstTime) {
                        firstTime = false;
                        return;
                    }
                    var _data = {};
                    $($("#J_Search").serializeArray()).each(function () {//获取查询条件
                        _data[this.name] = this.value;
                    });
                    _data.pageNo = aoData[3].value / aoData[4].value + 1;
                    _data.pageSize = aoData[4].value;
                    CRM.request({
                        "type": 'GET',
                        "url": sSource,
                        "dataType": "json",
                        contentType: "application/x-www-form-urlencoded",
                        xhrFields: {
                            withCredentials: true
                        },
                        "data": _data,
                        contentType: "application/json",
                        "success": function (req) {
                            if (req.status) {
                                var resultData = CRM.getDataTabelDtaObject();
                                if (req.entry) {
                                    resultData.iTotalDisplayRecords = 9999999;
                                    resultData.iTotalRecords = 9999999;
                                    $.each(req.entry.dayBillVoList, function (key, entry) {
                                        resultData.aaData.push(entry)
                                    });
                                    $.each(req.entry.bankAccount, function (key, entry) {
                                        $("#" + key).text(entry);
                                    });
                                } else {
                                    CRM.showDialog(req.message);
                                }
                                fnCallback(resultData);
                            } else {
                                CRM.showDialog(req.message);
                            }
                        }
                    });
                }, //给列表排序 ，第一个参数表示数组 (由0开始)。1 表示Browser列。第二个参数为 desc或是asc
                "aoColumns": [
                    {
                        "sTitle": "日期",
                        "mDataProp": "day",
                        "bSortable": true
                    },
                    {
                        "sTitle": "资金变化",
                        "mDataProp": "totalFee",
                        "bSortable": true
                    },
                    {
                        "sTitle": "操作",
                        "bSortable": true,
                        "mRender": function (data, type, row) {
                            var _returnStr = "";
                            _returnStr += '<button type="button" class="btn btn-info" onclick="javascript:showInfo(\'' + row.day + '\')">详 情</button>';
                            return _returnStr;
                        }
                    }

                ], //列设置，表有几列，数组就有几项
                "bStateSave": true, //保存状态到cookie *************** 很重要 ， 当搜索的时候页面一刷新会导致搜索的消失。使用这个属性就可避免了
                "sPaginationType": "simple" //分页，一共两种样式，full_numbers和two_button(默认)

            });
        },
        /**
         * 初始化详情的列表
         */
        initInfoTable: function () {
            var self = this;
            $("#shop_bank_info_table").dataTable({
                "oLanguage": CRM.pageConfig,
                "sScrollX": "100%",   //表格的宽度
                "sScrollXInner": "100%",   //表格的内容宽度
                "bScrollCollapse": true,  //当显示的数据不足以支撑表格的默认的高度时，依然显示纵向的滚动条。(默认是false)
                "bPaginate": true,  //是否显示分页
                "bLengthChange": false,  //每页显示的记录数
                "bFilter": false, //搜索栏
                "bSort": false, //是否支持排序功能
                "bInfo": false, //显示表格信息
                "bAutoWidth": false,  //自适应宽度
                "bServerSide": true,//开启服务端处理
                "sAjaxSource": CRM.getOuterUrl('/centralbank/caiwu/bankBillList'),
                "fnInitComplete": function () {
                    this.fnAdjustColumnSizing(true);
                },
                "fnServerData": function (sSource, aoData, fnCallback) {
                    if (infoFirstTime) {
                        infoFirstTime = false;
                        return;
                    }
                    var _data = {};
                    $($("#J_Search").serializeArray()).each(function () {//获取查询条件
                        _data[this.name] = this.value;
                    });
                    _data.pageNo = aoData[3].value / aoData[4].value + 1;
                    _data.pageSize = aoData[4].value;
                    _data.day = self._day;
                    CRM.request({
                        "type": 'GET',
                        "url": sSource,
                        "dataType": "json",
                        contentType: "application/x-www-form-urlencoded",
                        xhrFields: {
                            withCredentials: true
                        },
                        "data": _data,
                        contentType: "application/json",
                        "success": function (req) {
                            if (req.status) {
                                var resultData = CRM.getDataTabelDtaObject();
                                if (req.entry) {
                                    resultData.iTotalDisplayRecords = 9999999;
                                    resultData.iTotalRecords = 9999999;
                                    $.each(req.entry, function (key, entry) {
                                        if(!entry.bizOrderDO){
                                            entry.bizOrderDO = {};
                                            entry.bizOrderDO.itemName = "";
                                            entry.bizOrderDO.bizOrderId = "";
                                        }
                                        resultData.aaData.push(entry)
                                    });
                                } else {
                                    CRM.showDialog(req.message);
                                }
                                fnCallback(resultData);
                            } else {
                                CRM.showDialog(req.message);
                            }
                        }
                    });
                }, //给列表排序 ，第一个参数表示数组 (由0开始)。1 表示Browser列。第二个参数为 desc或是asc
                "aoColumns": [
                    {
                        "sTitle": "账单类型",
                        "mDataProp": "bankBill.billTypeStr",
                        "bSortable": true
                    },
                    {
                        "sTitle": "结算时间",
                        "mDataProp": "bankBill.formatGmtCreate",
                        "bSortable": true
                    },
                    {
                        "sTitle": "结算金额",
                        "mDataProp": "bankBill.totalFee",
                        "bSortable": true
                    },
                    {
                        "sTitle": "实付",
                        "mDataProp": "actuayPayFund",
                        "bSortable": true
                    },
                    {
                        "sTitle": "红包",
                        "mDataProp": "coupon",
                        "bSortable": true
                    },
                    {
                        "sTitle": "套餐",
                        "mDataProp": "packageFund",
                        "bSortable": true
                    },
                    {
                        "sTitle": "特价",
                        "mDataProp": "specialPrice",
                        "bSortable": true
                    },
                    {
                        "sTitle": "满减",
                        "mDataProp": "fullReduce",
                        "bSortable": true
                    },
                    {
                        "sTitle": "物流费",
                        "mDataProp": "deliveryService",
                        "bSortable": true
                    },
                    {
                        "sTitle": "配送费",
                        "mDataProp": "originFreightCharge",
                        "bSortable": true
                    },
                    {
                        "sTitle": "订单名称",
                        sWidth:80,
                        "mDataProp": "bizOrderDO.itemName",
                        "bSortable": true
                    },
                    {
                        "sTitle": "订单ID",
                        "mDataProp": "bizOrderDO.bizOrderId",
                        "bSortable": true
                    }

                ], //列设置，表有几列，数组就有几项
                "bStateSave": true, //保存状态到cookie *************** 很重要 ， 当搜索的时候页面一刷新会导致搜索的消失。使用这个属性就可避免了
                "sPaginationType": "simple" //分页，一共两种样式，full_numbers和two_button(默认)

            });
        },
        /**
         * 查看详情
         * @param day
         */
        showInfo: function (day) {
            this._day = day;
            $("#shop_order_dialog").modal('show');
        },
        /**
         * 表单验证初始化
         */
        initValidator: function () {
            $('#J_Search').bootstrapValidator({
                message: '输入错误',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                }
            });
        },
        bindEvent: function () {
            var self = this;
            $("#search_btn").on('click', function () {
                self.doSearch();
            });
            $("#shop_order_dialog").on('shown.bs.modal', function (e) {
                $('#shop_bank_info_table').DataTable().ajax.reload();
            });
        }
    };

    window.doSearch = $.proxy(shopBank.doSearch, shopBank);
    window.showInfo = $.proxy(shopBank.showInfo, shopBank);

    shopBank.init();
});